﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Provider
{
    public class ObjectProvider : GenericProvider, IObjectProvider
    {
        public IEnumerable<ApplicationObject> SearchObjects(out int totalRecord, Guid applicationKey, string name = null, string parentIdentifier = null, int pagesize = 0, int pageindex = 0)
        {
            var appkey = applicationKey.ToString().ToUpper();
            var query = from o in repository.Objects where o.ApplicationKey.ToUpper() == appkey select o;
            //query = query.Where(x => x.Active.Equals("Y") || x.Active.Equals("C"));
            query = query.OrderBy(o => new {o.Olevel }) as IQueryable<ApplicationObject>;
            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.Where(x => x.ObjectNameEnglish.ToUpper().Contains(name.Trim().ToUpper()) || x.ObjectNameLocal.ToUpper().Contains(name.Trim().ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(parentIdentifier))
            {
                query = query.Where(x => x.ParentId.ToUpper().Equals(parentIdentifier.Trim().ToUpper()));
            }
            //query = query.OrderBy(x => x.OrderId);
            totalRecord = query.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }

            return query.ToList();
        }


        public bool Update(ApplicationObject obj)
        {
            try
            {
                var appKey = obj.ApplicationKey.ToUpper();
                var objectId = obj.Id;
                var objectChecked = repository.FindOne<ApplicationObject>(x => x.Id == objectId && x.ApplicationKey.ToUpper() == appKey);
                if (objectChecked == null) return false;

                objectChecked.Active = obj.Active;
                objectChecked.ObjectIconUrl = obj.ObjectIconUrl;
                objectChecked.ObjectNameEnglish = obj.ObjectNameEnglish;
                objectChecked.ObjectNameLocal = obj.ObjectNameLocal;
                objectChecked.ObjectUrl = obj.ObjectUrl;
                objectChecked.Olevel = obj.Olevel;
                objectChecked.OrderId = obj.OrderId;
                objectChecked.ParentId = obj.ParentId;

                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(ObjectSubstitution obj)
        {
            try
            {
                var appKey = obj.ApplicationKey.ToUpper();
                var objectId = obj.Id;
                var objectChecked = repository.FindOne<ObjectSubstitution>(x => x.Id == objectId && x.ApplicationKey.ToUpper() == appKey);
                if (objectChecked == null) return false;

                objectChecked.Active = obj.Active;
                objectChecked.ObjectNameEnglish = obj.ObjectNameEnglish;
                objectChecked.ObjectNameLocal = obj.ObjectNameLocal;
                objectChecked.Olevel = obj.Olevel;
                objectChecked.OrderId = obj.OrderId;
                objectChecked.ParentId = obj.ParentId;

                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(Guid applicationKey, string objectId)
        {
            try
            {
                var appKey = applicationKey.ToString().ToUpper();
                repository.Delete<ApplicationObject>(x => x.Id == objectId && x.ApplicationKey.ToUpper() == appKey);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Add(ApplicationObject obj)
        {
            try
            {
                repository.Objects.Add(obj);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Add(ObjectSubstitution obj)
        {
            try
            {
                repository.Add<ObjectSubstitution>(obj);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
